<template>
  <div class="case-query-page">
    <div class="title" slot="header">
      <span class="text">自由裁量列表</span>
      <list-search :cols="columns" @search="searchParamChange"/>
      <head-btns @load="pageBtns" @add="add(typelist.t1)" @mod="mod(typelist.t2)" @del="del"/>
    </div>
    <list-table ref="listTable" @selected="listSelected"/>
    <detail-info
            v-model="detailVisible"
            ref="detailFrom"
            @submit="getTableData"
    />
  </div>
</template>
<script>
  import {mapState, mapActions} from 'vuex'
  import utils from '@/assets/utils/zhzf-util';
  import casecauseApi from "@/api/system/casecause";
  import discretionApi from "@/api/system/discretion";
  import detailInfo from './detail-info';
  import HeadBtns from "@/views/comps/zhzf/head-btns";
  import ListSearch from "../../comps/zhzf/list-search";
  import ListTable from "@/views/comps/zhzf/list-table";
  import {mapMutations} from "vuex";
  import CustBtnCol from '@/views/comps/zhzf/list-table-col-btn';

  export default {
    name: "UserPage",
    components: {ListSearch, ListTable, HeadBtns, detailInfo, CustBtnCol},
    data() {
      return {
        query: {},
        loading: false,
        columns: [
          {title: '违法行为', key: 'caseCauseName', width: '320'},
          {title: '违法程度', key: 'wfcd', width: '100'},
          {title: '违法情节', key: 'wfqj'},
          {title: '处罚标准', key: 'cfbz'},
          {title: '罚款下限', key: 'lawerLimit'},
          {title: '罚款上限', key: 'upperLimit'},
          {
            title: '操作',
            key: 'op',
            component: {
              name: CustBtnCol,
              props: {
                items: [
                  {text: '修改', emit: 'mod'},
                  {text: '删除', emit: 'del'}
                ]
              }
            }
          }
        ],
        data: [],
        breakdroplist: '',
        rowHandle: {},
        options: {
          stripe: true,
          highlightCurrentRow: true
        },
        pagination: {
          currentPage: 1,
          pageSize: 15,
          total: 0
        },
        user: {},
        detailVisible: false,
        fromclose: false,
        selected: [],
        typelist:{
          t1:'1',
          t2:'2'
        }
      };
    },
    computed: {
      ...mapState('d2admin/user', [
        'info'
      ])
    },
    mounted() {
      this.$bus.$off('mod').$on('mod', res => {
        this.selected = [res];
        this.mod();
      });
      this.$bus.$off('del').$on('del', res => {
        this.selected = [res];
        this.del();
      });
      this.initPage();
      this.getdepData();
      // this.getTableData();
    },
    methods: {
      ...mapMutations({
        pageInit: "d2admin/page/init"
      }),
      pageBtns(btns) {
        btns = btns || [];
        this.rowHandle = {
          width: 150, custom: []
        };
        btns.forEach(item => {
          if (item.url == 'mod') {
            this.rowHandle.custom.push({
              title: item.name,
              size: 'mini',
              emit: `${item.url}-model`,
              icon: item.icon,
              style: `color:${item.url == 'del' ? '#f53c1b' : '#1B98F5'}`
            });
          }
        })
      },
      listSelected(list) {
        this.selected = list;
      },
      async searchParamChange(query) {
        await this.$refs.listTable.reloadData(query);
      },
      async getdepData(){//获取违法行为
        let res = await casecauseApi.dropList({pageIndex:0,pageSize:1000,asc:true});
        this.breakdroplist = res.data.rows;
      },
      showDetail(row) {
        row = row || {};
        this.detailVisible = true;
        this.$refs.detailFrom.getdepData({breakdroplist: this.breakdroplist, info: JSON.parse(JSON.stringify(row))});
      },
      async getTableData() {
        this.initPage();
        this.$forceUpdate()
      },
      async initPage() {
        await this.$refs.listTable.initPage({
          method: discretionApi.pageList,
          cols: this.columns,
          noPage:false
        })
      },
      add() {
        let type = this.typelist.t1;
        this.detailVisible = true;
        this.$refs.detailFrom.getdepData({breakdroplist: this.breakdroplist,handletype:type});
      },
      mod() {
        let type = this.typelist.t2;
        if (this.selected.length != 1) {
          utils.showTips("请选择一个数据", false);
          return;
        }
        this.detailVisible = true;
        this.$refs.detailFrom.getdepData({breakdroplist: this.breakdroplist, handletype:type,info: JSON.parse(JSON.stringify(this.selected[0]))});
      },
      del() {
        if (!this.selected.length) {
          utils.showTips("请选择要删除的数据", false);
          return;
        }
        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(async () => {
          let delIds = [];
          this.selected.forEach(item => {
            delIds.push(item.caseDiscretionId);
          });
          let res = await discretionApi.del(delIds);
          if (res.ok) {
            utils.showTips(res.msg);
          }
          this.getTableData()
        });
      },
    }
  };
</script>
